package cn.edu.hqu.cst.android.wzc.experiment5;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public static int MODE=MODE_PRIVATE;
    public static final String PREFERENCE_NAME="SaveSetting";
    Button btnLogin;
    EditText etAccount,etPassword;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    String account,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnLogin=findViewById(R.id.btn_login);
        etAccount=findViewById(R.id.account_input);
        etPassword=findViewById(R.id.password_input);
        sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
        editor=sharedPreferences.edit();
        boolean ifexist_1 =sharedPreferences.contains("account");
        boolean ifexist_2 =sharedPreferences.contains("password");
        btnLogin.setOnClickListener(this);
        //如果已经输入过账号密码就把其设置为编辑框的默认值
        if (ifexist_1&&ifexist_2){
            etAccount.setHint(sharedPreferences.getString("account",null));
            etPassword.setHint(sharedPreferences.getString("password",null));
        };

/**完成代码***/
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_login:
                if(etAccount.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this,"未输入账号",Toast.LENGTH_SHORT).show();
                    break;
                }
                if(etPassword.getText().toString().equals("")){
                    Toast.makeText(MainActivity.this,"未输入密码",Toast.LENGTH_SHORT).show();
                    break;
                }
                editor.putString("account",etAccount.getText().toString());
                editor.putString("password",etPassword.getText().toString());
                Toast.makeText(MainActivity.this,"记录账号密码成功，以后启动后自动填充该账户和密码",Toast.LENGTH_SHORT).show();
                editor.apply();




/**完成代码***/
                break;
        }
    }
}



